package indi.zhifa.study2025.test.rabbitmq.route.producer;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson2.JSON;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import indi.zhifa.engine.core.common.utils.TimeUtil;
import indi.zhifa.study2025.test.rabbitmq.common.Constant;
import indi.zhifa.study2025.test.rabbitmq.common.entity.ItemInfoChangeMsg;
import indi.zhifa.study2025.test.rabbitmq.common.entity.OrderMsg;
import indi.zhifa.study2025.test.rabbitmq.common.entity.SalesChangeMsg;
import indi.zhifa.study2025.test.rabbitmq.common.utils.NWRabbitUtils;

import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeoutException;

public class RouteProcedureApplication {

    public static void main(String[] args) throws IOException, TimeoutException {
        Channel channel = null;
        Connection connection = null;
        try{
            connection = NWRabbitUtils.getConnection();
            channel = connection.createChannel();
            // 声明交换机, BuiltinExchangeType.DIRECT 是路由模式
            channel.exchangeDeclare(Constant.ROUTE_EXCHANGE_NAME, BuiltinExchangeType.DIRECT,true);
            // 如果队列不存在，则创建队列
            channel.queueDeclare(Constant.ROUTE_QUE_NAME1, true, false, false, null);
            channel.queueDeclare(Constant.ROUTE_QUE_NAME2, true, false, false, null);

            // 绑定队列与交换机
            channel.queueBind(Constant.ROUTE_QUE_NAME1,Constant.ROUTE_EXCHANGE_NAME,Constant.SALES_CHANGE_KEY);
            channel.queueBind(Constant.ROUTE_QUE_NAME1,Constant.ROUTE_EXCHANGE_NAME,Constant.ITEM_INFO_CHANGE_KEY);
            channel.queueBind(Constant.ROUTE_QUE_NAME2,Constant.ROUTE_EXCHANGE_NAME,Constant.ITEM_INFO_CHANGE_KEY);
            channel.queueBind(Constant.ROUTE_QUE_NAME2,Constant.ROUTE_EXCHANGE_NAME,Constant.BILL_CONSIGN_FINISH);

            Channel finalChannel = channel;

            CountDownLatch countDownLatch = new CountDownLatch(3);

            // 模拟商品名变化数据
            new Thread(() -> {
                for (int i = 0; i < 25; i++) {
                    ItemInfoChangeMsg itemInfoChangeMsg = new ItemInfoChangeMsg();
                    Long id = IdUtil.getSnowflakeNextId();
                    itemInfoChangeMsg.setItemId(id);
                    itemInfoChangeMsg.setItemOrgName("old_"+RandomUtil.randomString(8));
                    itemInfoChangeMsg.setItemNewName("new_"+RandomUtil.randomString(8));
                    String jsonMsg = JSON.toJSONString(itemInfoChangeMsg);
                    try {
                        finalChannel.basicPublish(Constant.ROUTE_EXCHANGE_NAME, Constant.ITEM_INFO_CHANGE_KEY, null, jsonMsg.getBytes());
                        System.out.println("push msg 商品名字变化"+jsonMsg);
                        Thread.sleep(200);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }

                }
                countDownLatch.countDown();
            }).start();

            // 模拟销售名变化数据
            new Thread(() -> {
                for (int i = 0; i < 10; i++) {
                    SalesChangeMsg salesChangeMsg = new SalesChangeMsg();
                    Long id = IdUtil.getSnowflakeNextId();
                    salesChangeMsg.setId(id);
                    salesChangeMsg.setOrgName("old_"+RandomUtil.randomString(8));
                    salesChangeMsg.setNewName("new_"+RandomUtil.randomString(8));
                    String jsonMsg = JSON.toJSONString(salesChangeMsg);
                    try {
                        finalChannel.basicPublish(Constant.ROUTE_EXCHANGE_NAME, Constant.SALES_CHANGE_KEY, null, jsonMsg.getBytes());
                        System.out.println("push msg 销售员名字变化"+jsonMsg);
                        Thread.sleep(200);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
                countDownLatch.countDown();
            }).start();

            // 模拟订单完结
            new Thread(() -> {
                for (int i = 0; i < 100; i++) {
                    String code = RandomUtil.randomString(8);
                    OrderMsg orderMsg = new OrderMsg();
                    orderMsg.setCode(code);
                    orderMsg.setBillTime(LocalDateTime.now());
                    orderMsg.setBillTimeSecond(TimeUtil.nowSecond());
                    orderMsg.setAmount(RandomUtil.randomBigDecimal(BigDecimal.valueOf(10), BigDecimal.valueOf(1000)));

                    String jsonMsg = JSON.toJSONString(orderMsg);
                    try{
                        finalChannel.basicPublish(Constant.ROUTE_EXCHANGE_NAME, Constant.BILL_CONSIGN_FINISH, null, jsonMsg.getBytes());
                        System.out.println("push msg 订单完结"+orderMsg);
                        Thread.sleep(200);
                    }catch (IOException e) {
                        throw new RuntimeException(e);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                countDownLatch.countDown();
            }).start();

            // 等待完成
            countDownLatch.await();

        }catch (Exception e){
            System.out.println(e);
        }finally {
            channel.close();
            connection.close();
        }
    }
}
